<template>
  <div v-loading="tableLoading" class="gl_contentBox gl_heightFull">
    <!-- 筛选项 -->
    <el-row :gutter="20" class="gl_searchBox" style="margin: 0 0 15px 0;">
      <el-col :span="6" :lg="4">
        <p>审核状态</p>
        <el-select v-model="form.ApproveStatusCode" size="mini" placeholder="请选择审核状态" filterable @change="getList()">
          <el-option v-for="item in dictAll.trainningApproveProcessStatus" :key="item.code" :value="item.code" :label="item.value" />
        </el-select>
      </el-col>
      <el-col :span="8" :lg="6">
        <p>培训时间</p>
        <el-date-picker
          v-model="filterDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 100%;"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          size="mini"
          @change="handleTimeChange"
        />
      </el-col>
      <el-col :span="6" :lg="4">
        <p>培训类型</p>
        <el-select v-model="form.TrainningTypeId" size="mini" placeholder="请选择培训类型" filterable @change="getList()">
          <el-option label="全部" value="" />
          <el-option v-for="item in typeList" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-col>
      <el-col :span="6" :lg="4">
        <p>培训名称</p>
        <el-input v-model="form.Filter" placeholder="请输入" size="mini" clearable @change="getList()" />
      </el-col>
      <el-col :span="6" :lg="6" align="right" class="search_btn">
        <!-- 查询按钮 -->
        <el-button type="primary" size="mini" @click="getList()">查询</el-button>
        <!-- 重置按钮 -->
        <el-button type="warning" size="mini" @click="initForm()">重置</el-button>
      </el-col>
    </el-row>
    <el-row v-if="false" class="gl_fs_14 gl_mb_5" style="color: #595959">
      <el-col align="right">共计 {{ total }} 条数据</el-col>
    </el-row>
    <el-table class="gl_table_noBorder gl_mb_15 game_table_list" :data="tableData" stripe size="mini">
      <el-table-column type="index" label="序号" align="center" :width="60" />
      <el-table-column label="logo" align="center" :width="70">
        <template slot-scope="{ row }">
          <img class="game_coverimg" :src="getStaticPhotoUrl(row.logoUrl)" alt="" @error="errorPhoto">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="培训名称" :min-width="180" />
      <el-table-column prop="trainningType.name" label="培训类型" :min-width="120" />
      <el-table-column prop="address" label="培训地址" :min-width="200">
        <template slot-scope="{ row }">
          {{ row.provinceName }} {{ row.cityName }} {{ row.countyName }} {{ row.address }}
        </template>
      </el-table-column>
      <el-table-column label="培训时间" :min-width="180">
        <template slot-scope="{ row }">
          {{ row.startTime && row.startTime.substring(0, 10) }} 到 {{ row.endTime && row.endTime.substring(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column label="审核状态" :width="110">
        <template slot-scope="{ row }">
          <span>
            <span v-if="row.approveResult" class="gl_pl_5">{{ row.approveResult.name }}</span>
            <span v-else class="gl_pl_5">{{ row.approveStatus.name }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="left" :width="140">
        <template slot-scope="{ row }">
          <el-button :disabled="row.approveStatus.code != 'NotApproved'" size="mini" type="text" @click="openApprove(row, 'list')">
            <span class="gl_color_primary">审核</span>
          </el-button>
          <el-button size="mini" type="text" @click="enterToApproveDetail(row)">
            <span class="gl_color_primary">查看详情</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-row style="text-align: right;" class="gl_pb_15 gl_mr_15">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size="form.MaxResultCount"
        :current-page="form.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
    <Approve ref="approve" />
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import Approve from '../identifyApprove/approve.vue';
import defaultTrainLogo from '@/assets/images/game/game_logoDefaultPhoto.png';
import { Api_GET_TrainTrainningApproveApproveTrainningsQO } from '@/lib/form/train/swaggerForm';
import {
  api_GET_TrainTrainningType,
  api_GET_TrainTrainningApproveApproveTrainnings
} from '@/extend/api/trainswaggerApi/app';
export default {
  components: {
    Approve
  },
  data() {
    return {
      defaultTrainLogo: defaultTrainLogo,
      form: new Api_GET_TrainTrainningApproveApproveTrainningsQO(),
      tableData: [],
      filterDate: [], // 培训时间
      typeList: [], // 培训类型
      total: 0,
      tableLoading: false
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      dictAll: state => state.dict.dictAll
    })
  },
  created() {
    this.getType();
    this.getDict();
    this.initForm();
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    // 获取培训类型
    getType() {
      var that = this;
      api_GET_TrainTrainningType().then(res => {
        that.typeList = res.items;
      });
    },
    // 初始化
    initForm() {
      this.form = new Api_GET_TrainTrainningApproveApproveTrainningsQO();
      this.filterDate = [];
      this.getList();
    },
    // 获取培训列表
    getList(page) {
      this.form.Page = page || 1;
      this.form.SkipCount = (this.form.Page - 1) * this.form.MaxResultCount;
      this.form.Sorting = 'SubmitTime desc'; // 按提交时间倒序
      this.tableLoading = true;
      api_GET_TrainTrainningApproveApproveTrainnings(this.form).then(res => {
        this.tableLoading = false;
        this.tableData = res.items || [];
        this.total = res.totalCount;
      }, error => {
        this.tableLoading = false;
        this.$message({ type: 'error', message: '获取培训列表失败！' + (error.error && error.error.message || '') });
      });
    },
    // 处理时间筛选项
    handleTimeChange(time) {
      this.form.StartTime = time && time[0];
      this.form.EndTime = time && time[1];
      this.getList();
    },
    // 点击审核
    openApprove(row, code) {
      this.$refs.approve.open(row, code);
    },
    // 进入培训审核详情
    enterToApproveDetail(info) {
      const approve = {
        approveId: info.trainningApproveId
      };
      this.$router.push({
        name: 'TrainIdentifyApproveDetail',
        params: approve
      });
    },
    // 获取图片地址
    getStaticPhotoUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultTrainLogo;
    },
    // 分页
    changePage(page) {
      this.getList(page);
    },
    changePageSize(pagesize) {
      this.form.MaxResultCount = pagesize;
      this.getList();
    }
  }
};
</script>
<style>
.game_table_list .game_coverimg {
  width: 36px;
  height: 36px;
  border-radius: 18px;
  overflow: hidden;
}
</style>
